package com.snj.test;

import java.time.LocalDate;
import java.time.YearMonth;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

public class DateListGenerator {

    public static void main(String[] args) {
        String inputYearMonth = "2024-10";
        List<String> dateList = getPreviousDates(inputYearMonth);
        System.out.println(dateList.size());
        dateList.forEach(System.out::println);
    }

    public static List<String> getPreviousDates(String inputYearMonth) {
        List<String> dates = new ArrayList<>();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM");

        // Parse the input year-month
        YearMonth yearMonth = YearMonth.parse(inputYearMonth, formatter);

        // Get the first day of the month
        LocalDate firstOfMonth = yearMonth.atDay(1);

        // Get the current date
        LocalDate currentDate = LocalDate.now();

        // Determine the last day to iterate
        LocalDate lastDayToInclude = yearMonth.atEndOfMonth();
        if (currentDate.isBefore(lastDayToInclude)) {
            lastDayToInclude = currentDate;
        }

        // Iterate from the first day of the month until the current date
        for (LocalDate d = firstOfMonth; !d.isAfter(lastDayToInclude); d = d.plusDays(1)) {
            dates.add(d.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        }

        return dates;
    }
}